package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.AddressMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.model.Address;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.bo.SaveAddressDetailBO;
import com.cskaoyan.model.vo.AddressDataVO;
import com.cskaoyan.model.vo.CollectorDataVO;
import com.cskaoyan.model.vo.CollectorVO;
import com.cskaoyan.model.vo.ExceptionVO;
import com.cskaoyan.service.WxAddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @program: 微信小程序
 * @description:
 * @author: Wang
 * @create: 2022-05-09 15:11
 **/

@Repository
public class WxAddressServiceImpl implements WxAddressService {

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    UserMapper userMapper;

    /*
     * description 获取用户信息
     * @author Wangtianhao
     * @date 2022-05-10 14:21
     */
    public Object getUserInfo() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            //在已经认证成功的情况下，可以获得用户信息
            // 获得的用户信息的来源 → 来源realm的doGetAuthenticationInfo方法的返回值的第一个参数
            Object primaryPrincipal = subject.getPrincipals().getPrimaryPrincipal();
            return primaryPrincipal;
        } else {
            ExceptionVO.error(700, "账号未登录");
        }
        return 0;
    }

    /*
     * description 获取地址列表
     * @author Wangtianhao
     * @date 2022-05-09 19:59
     */
    @Override
    public AddressDataVO queryList(BaseParamBO param) {
        String username = String.valueOf(getUserInfo());
        Integer userIds = userMapper.selectUserId(username);
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        List<Address> list = addressMapper.queryWxAddressList(param, userIds);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<Address> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return AddressDataVO.data(total, pages, limit, page, list);
    }

    /*
     * description 获取地址信息
     * @author Wangtianhao
     * @date 2022-05-09 19:59
     */
    @Override
    public Address getAddressDetail(Integer id) {
        Address address = addressMapper.getAddressDetail(id);

        String province1 = addressMapper.getReignName(Integer.valueOf(address.getProvince()));
        String city1 = addressMapper.getReignName(Integer.valueOf(address.getCity()));
        String county1 = addressMapper.getReignName(Integer.valueOf(address.getCounty()));

        address.setProvince(province1);
        address.setCity(city1);
        address.setCounty(county1);

        return address;
    }

    /*
     * description 保存地址信息
     * @author Wangtianhao
     * @date 2022-05-09 19:59
     */

    @Override
    public void saveAddressDetail(SaveAddressDetailBO saveAddressDetailBO) {
        String username = (String) getUserInfo();
        Integer userIds = userMapper.selectUserId(username);

        String city = saveAddressDetailBO.getCity();
        String county = saveAddressDetailBO.getCounty();
        String province = saveAddressDetailBO.getProvince();

        String addressDetail = saveAddressDetailBO.getAddressDetail();

        String addressDetail1 = province + city + county + addressDetail;
        String province1 = Integer.toString(addressMapper.getReignId(province));

        String city1 = Integer.toString(addressMapper.getReignCityId(addressMapper.getReignId(province), city));
        String county1 = Integer.toString(addressMapper.getReignCountyId(addressMapper.getReignCityId(addressMapper.getReignId(province), city), county));

        // 修改地址
        if (saveAddressDetailBO.getId() != 0) {
            if (saveAddressDetailBO.getIsDefault()) {
                addressMapper.setIsDefault(userIds);
            }
            addressMapper.saveAddressDetail(city1, county1, province1, addressDetail1, saveAddressDetailBO);
        } else {
            if (saveAddressDetailBO.getIsDefault()) {
                addressMapper.setIsDefault(userIds);
            }
            addressMapper.saveNewAddressDetail(userIds, city1, county1, province1, addressDetail1, saveAddressDetailBO);
        }


//        addressMapper.saveAddressDetail(city1, county1, province1, addressDetail1, saveAddressDetailBO);
//        if (saveAddressDetailBO.getIsDefault()) {
//            addressMapper.setIsDefault(saveAddressDetailBO.getId());
//        }
//        if (saveAddressDetailBO.getId() != 0) {
//            addressMapper.saveAddressDetail(city1, county1, province1, addressDetail1, saveAddressDetailBO);
//            if (saveAddressDetailBO.getIsDefault()) {
//                addressMapper.setIsDefault(saveAddressDetailBO.getId());
//            }
//        } else {
//            addressMapper.saveNewAddressDetail(userIds, city1, county1, province1, addressDetail1, saveAddressDetailBO);
//            if (saveAddressDetailBO.getIsDefault()) {
//                addressMapper.setIsDefault(saveAddressDetailBO.getId());
//            }
//        }
    }

    /*
     * description 删除地址信息
     * @author Wangtianhao
     * @date 2022-05-09 19:59
     */
    @Override
    public void deleteAddressDetail(Integer id) {
        addressMapper.deleteAddressDetail(id);
    }

}
